package com.kxr.day09.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kxr.day09.dao.NewsDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(urlPatterns = "/dashboard")
public class DashboardServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        NewsDao dao = new NewsDao();
        List<JSONObject> list = dao.sourceCount();
        req.setAttribute("sourceCount", JSON.toJSONString(list));
        List<JSONObject> timelineNewsNums = dao.timelineNewsNum();
        List<String> timeArr = new ArrayList<>();
        List<String> valueArr = new ArrayList<>();
        for (int i = timelineNewsNums.size() - 1; i >= 0; i--) {
            timeArr.add(timelineNewsNums.get(i).getString("name"));
            valueArr.add(timelineNewsNums.get(i).getString("value"));
        }
        req.setAttribute("timeArr", JSON.toJSONString(timeArr));
        req.setAttribute("valueArr", JSON.toJSONString(valueArr));

        req.getRequestDispatcher("/dashboard.jsp").forward(req, resp);
    }
}
